package com.ticket;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLOutput;
import java.util.InputMismatchException;
import java.util.Scanner;


public class UserRegister {
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;
    private static Scanner scanner = new Scanner(System.in);


    public static void userRegister() {


        String username, password, confirmPassword, phone = null, sex, money = null, realName;

        int power;

        String passwordRegex = "^(?=.*[0-9])(?=.*[a-zA-Z]).{6,}$";

        String phoneRegex = "^1[3-9]\\d{9}$";

        String MoneyRegex = "\\$?\\d+(\\.\\d{1,2})?$";

        while (true) {
            System.out.println("请输入：0.用户注册\t1.管理员注册\t-1.返回");
            power = scanner.nextInt();
            if (power == 0 || power == 1) {
                break;
            } else if (power == -1) {
                HomePage.home();
                break;
            } else {
                System.out.println("请输入0或1");
            }
        }
        if (power == 0 || power == 1) {
            do {
                System.out.println("请输入用户名：");
                username = scanner.next();

                if (checkUsernameExist(username)) {
                    System.out.println("该用户已经被注册，请重新选择");
                } else {
                    break;
                }
            } while (true);

            do {
                System.out.println("请输入密码：");
                password = scanner.next();
                if (!password.matches(passwordRegex)) {
                    System.out.println("密码格式不正确，密码至少6位，包含字母和数字。请重新输入。");
                    continue;
                }
                System.out.println("请确认密码：");
                confirmPassword = scanner.next();

                if ((!password.equals(confirmPassword))) {
                    System.out.println("两次密码不一致，请重新输入");
                } else {
                    break;
                }
            } while (true);


            System.out.println("请输入真实姓名：");
            realName = scanner.next();

            boolean isSuccess = false;
            while (!isSuccess) {
                System.out.println("请输入电话：");
                phone = scanner.next();
                if (!phone.matches(phoneRegex)) {
                    System.out.println("手机号格式不正确，请输入正确手机号码。请重新输入。");
                    continue;
                }
                isSuccess = true;
            }

            do {
                System.out.println("请输入性别：");
                sex = scanner.next();

                if (isValidGender(sex)) {
//                    System.out.println("性别输入有效");
                    break;
                } else {
                    System.out.println("性别输入无效");
                }
            }while (true);


            isSuccess = false;
            while (!isSuccess) {
                System.out.println("请输入账户金额：");
                money= scanner.next();
                if (!money.matches(MoneyRegex)) {
                    System.out.println("账户金额格式不正确，请重新输入。");
                    continue;
                }
                isSuccess = true;
            }

            if (registerUser(username, password, realName, phone, sex, money, power)) {
                if (power == 0) {
                    System.out.println("用户注册成功");
                } else {
                    System.out.println("管理员注册成功");
                }
                HomePage.home();
            } else {
                if (power == 0) {
                    System.out.println("用户注册失败");
                } else {
                    System.out.println("管理员注册失败");
                }
            }


        }
    }

    public static  boolean isValidGender(String gender){
        return gender.equalsIgnoreCase("男") || gender.equalsIgnoreCase("女");
    }

    private static boolean checkUsernameExist(String username) {


        boolean flag = false;
        try {
            connection = DBUtils.getConnection();
            preparedStatement = connection.prepareStatement("SELECT  * FROM user WHERE username = ?");
            preparedStatement.setString(1, username);

            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                flag = true;
                break;
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        if (flag) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean registerUser(String username, String password, String realName, String phone, String sex, String money, int power) {

        int rows = 0;
        try {

            connection = DBUtils.getConnection();
            preparedStatement = connection.prepareStatement("INSERT INTO user(username,password,realname,phone,sex,money,power) VALUES (?,?,?,?,?,?,?)");
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.setString(3, realName);
            preparedStatement.setString(4, phone);
            preparedStatement.setString(5, sex);
            preparedStatement.setString(6, money);
            preparedStatement.setInt(7, power);

            rows = preparedStatement.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        }
        if (rows > 0) {
            return true;
        } else {
            return false;
        }

    }
}

